using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Tools
{
    public class FocalorsLoadScencePanel : MonoBehaviour
    {

        public Slider progressBar;
        public Text text;
        public float delayTime = 3;

        void Start()
        {
            progressBar = GetComponentInChildren<Slider>();
            text = GetComponentInChildren<Text>();
            StartCoroutine(LoadScene());
            
        }

        private void SetCurrentProgress(int progress)
        {
            text.text = progress + "%";
            progressBar.value = progress / 100f;
        }

        IEnumerator LoadScene()
        {
            int displayProgress = 0;
            int toProgress = 0;
            AsyncOperation op = SceneManager.LoadSceneAsync(FocalorsDataRWS.PlayerPrefsLoadData(Global.LoadScenceKey));
            op.allowSceneActivation = false;
            while (op.progress < 0.9f)
            {
                toProgress = (int)op.progress * 100;
                while (displayProgress < toProgress)
                {
                    ++displayProgress;
                    SetCurrentProgress(displayProgress);
                    yield return new WaitForEndOfFrame();
                }
            }

            toProgress = 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetCurrentProgress(displayProgress);
                yield return new WaitForEndOfFrame();
            }
            
            op.allowSceneActivation = true;
        }
    }
}

